package com.mydemo.likedy.controller.UserController;

import com.mydemo.likedy.Service.UserLoginService;
import com.mydemo.likedy.Utils.JwtUtils;
import com.mydemo.likedy.Utils.Result;
import com.mydemo.likedy.pojo.dto.EmailDTO;
import com.mydemo.likedy.pojo.dto.FindPasswordDTO;
import com.mydemo.likedy.pojo.dto.RegisterDTO;
import com.mydemo.likedy.pojo.dto.UserDTO;
import com.mydemo.likedy.pojo.entity.User;
import com.mydemo.likedy.pojo.vo.UserLoginVo;
import com.mydemo.likedy.properties.UserJwtProperties;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.mydemo.likedy.Constant.JwtClaimsConstant;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/luckyjourney/login")
@Slf4j
public class UserLoginController {
    @Autowired
    UserJwtProperties userJwtProperties;
    @Autowired
    UserLoginService userLoginService;

    /**
     * 登录系统
     * @param userDTO
     * @return
     */
    @PostMapping
    public Result<UserLoginVo> UserLogin(@RequestBody UserDTO userDTO) {
        User user = userLoginService.userLogin(userDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtils.createJWT(
                userJwtProperties.getUserSecretKey(),
                userJwtProperties.getUserTtl(),
                claims);
        UserLoginVo userLoginVo = UserLoginVo.builder().Username(user.getNickName()).token(token).build();


        return Result.success(userLoginVo, user.getId());
    }

    /**
     * 获得图片验证码
     * @param uuId
     * @param response
     * @throws IOException
     */

    @GetMapping("/captcha.jpg/{uuId}")
    public void SignUpCaptcha(@PathVariable String uuId, HttpServletResponse response) throws IOException {
        userLoginService.captcha(uuId, response);
    }

    /**
     * 获得邮箱验证码
     * @param emailDTO
     * @return
     */
    @PostMapping("/getCode")
    public Result SignUpCaptcha(@RequestBody @Validated EmailDTO emailDTO) {
        log.info("{}",emailDTO);
        userLoginService.validate(emailDTO);
        return  Result.success("发送成功");
    }

    /**
     * 检测邮箱验证码
     * @param email
     * @param code
     * @return
     */
    @PostMapping("/check")
    public Result check(String email,Integer code){
        userLoginService.checkCode(email,code);
        return Result.success("验证码成功");
    }

    /**
     * 注册
     * @param registerDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Validated RegisterDTO registerDTO) throws Exception {
        if (!userLoginService.register(registerDTO)) {
            return Result.error("注册失败,验证码错误");
        }
        return Result.success("注册成功");
    }


    @PostMapping("/findPassword")
    public Result findpassword(@RequestBody @Validated FindPasswordDTO findPasswordDTO) throws Exception {
        final Boolean b = userLoginService.findPassword(findPasswordDTO);
        return Result.success(b ? "修改成功" : "修改失败,验证码不正确");
    }
}
